package de.upb.aquarium.water.bubbles;

import java.util.LinkedList;
import java.util.List;

import de.upb.aquarium.World;
import de.upb.aquarium.api.utility.VectorFloat;

public class PumpBubbleSource extends BubbleSource {

	private VectorFloat initialMovement;

	private double spawnRatio;
	private double maxSpeed;
	private double maxDeviation;
	private double lastUpdateTime;
	private double timeToNextSpawn;
	
	public PumpBubbleSource(
			VectorFloat position,
			VectorFloat upDirection,
			VectorFloat initialMovement,
			double spawnRatio,
			double maxSpeed,
			double maxDeviation) {
		super(position, upDirection);
		
		this.spawnRatio		= spawnRatio;
		this.maxSpeed		= maxSpeed;
		this.maxDeviation	= maxDeviation;
		this.lastUpdateTime	= World.getInstance().getGlobalTime();
		this.timeToNextSpawn = 0.0;
		this.initialMovement = initialMovement;
	}

	@Override
	public List<WaterBubble> generate() {
		
		List<WaterBubble> result = new LinkedList<WaterBubble>();
		
		while (World.getInstance().getElapsedTime(lastUpdateTime) > timeToNextSpawn)
		{
			lastUpdateTime += timeToNextSpawn;
			
			timeToNextSpawn = -Math.log(Math.random())/spawnRatio;
			
			double deviationX	= maxDeviation*(2*Math.random() - 1.0);
			double deviationY	= 0.0;
			double deviationZ	= maxDeviation*(2*Math.random() - 1.0);
			
			VectorFloat initialMovement = this.initialMovement.add(
						new VectorFloat(new double[] {deviationX, deviationY, deviationZ})
					);
			
			result.add(new PumpWaterBubble(maxSpeed,
					   getPosition(),
					   getUpDirection(),
					   initialMovement));
		}
		
		return result;
	}
	
	public void reset()
	{
		lastUpdateTime = World.getInstance().getGlobalTime();
	}
}
